$(function() {
	activeNav($("#info-preSaleOrder"));
	
	$("#order-publish").click(function () {
		save($('#orderId').val());
	});
	
	$("#order-cancel").click(function () {
		clearDetail();
	});
	
	$("#order-close").click(function () {
		clearDetail();
	});
	
	$("#order-del").click(function () {
		del($('#orderId').val());
	});
	
	$("#orderStatus").change(function () {
		var status = $(this).val();
		switch(status) {
			case "1": status = "已支付预付款";
				$("#alipaySerialDiv").show();
				$("#documentSerialDiv").hide();
				$("#alipaySerial").focus();
				$("#alipaySerial").blur();
				break;
			case "3": status = "已完成"; 
				$("#documentSerialDiv").show();
				$("#alipaySerialDiv").hide();
				$("#documentSerial").focus();
				$("#documentSerial").blur();
				break;
		}
	});
	
});

function save(orderId) {
	var errors = $(".check-error");
	if (errors.length > 0) {
		return;
	}
	
	var status = $("#orderStatus").val();
	var documentSerial = $("#documentSerial").val();
	var alipaySerial = $("#alipaySerial").val();
	
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/preSaleOrder/ajaxSave',
		data : { orderId : orderId, status : status, documentSerial: documentSerial, alipaySerial: alipaySerial },
		success : function(data) {
			$.alert({message: data.message});
			clearDetail();
		},
		error : function() {
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
	$("#order-publish").attr("disabled",false);
}

function del(orderId) {
	if (confirm("废弃后不能恢复，确定要废弃吗？")) {
		$.ajax({
			async : false,
			cache : false,
			dataType : 'JSON',
			type : 'POST',
			url : ctx + '/admin/preSaleOrder/ajaxDel',
			data : { orderId : orderId },
			success : function(data) {
				$.alert({message: data.message});
				setTimeout(function () {
					searchForm.submit();
				}, 1000 * 3);
			},
			error : function() {
				$.alert({message: "操作异常，请稍候再试！"});
			}
		});
	}
}

function detail(orderId) {
	clearDetail();
	$('.dialog-win').mask('消息加载中，请稍后...');
	$('#order-content').html('');
	$('#order-body').show();
	$('#orderId').val(orderId);
	
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/preSaleOrder/ajaxDetail',
		data : { orderId : orderId },
		success : function(data) {
			$('.dialog-win').unmask();
			if (data.success) {
				var order = data.data;
				var html = new Array();
				var status = "";
				
				$("#orderNum").html(order.orderCode);
				switch(parseInt(order.status)) {
				case 0:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="0">待支付预付款</option>'
							+'<option value="1">已支付预付款</option>'
							+'<option value="3">已完成</option>'
						+'</select>');
					break;
				case 1:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="1">已支付预付款</option>'
							+'<option value="3">已完成</option>'
						+'</select>');
					break;
				case 3:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="3">已完成</option>'
						+'</select>');
					break;
				case 97:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="97">自动取消</option>'
						+'</select>');
					break;
				case 98:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="98">客户退订申请中</option>'
							+'<option value="100">退款成功</option>'
						+'</select>');
					break;
				case 99:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="99">已退订</option>'
						+'</select>');
					break;
				case 100:
					$("#orderStatus").html('<select id="orderStatus" name="status" >'
							+'<option value="100">退款成功</option>'
						+'</select>');
					break;
					default:
						$("#orderStatus").html('<select id="orderStatus" name="status" >'
								+'<option value="-1">数据错误</option>'
							+'</select>');
				}
				
				$("#orderStatus").val(order.status);
				$("#orderTime").html(new Date(order.orderTime).format("yyyy-MM-dd HH:mm:ss"));
				$("#payTime").html(new Date(order.payTime).format("yyyy-MM-dd HH:mm:ss"));
				$("#fullAmount").html("￥" + order.fullAmount);
				$("#preAmount").html("￥" + order.preAmount);
				$("#alipayAccount").html(order.alipayAccount);
				$("#contactName").html(order.contact.name);
				$("#contactMobile").html(order.contact.mobile);
				$("#alipaySerialSpan").html(order.alipaySerial);
				$("#documentSerialSpan").html(order.documentSerial);
				
				$("#alipaySerial").val(order.alipaySerial);
				$("#documentSerial").val(order.documentSerial);
				
				$.each(order.orderProducts, function (i, n) {
					var product = n.product;
					html.push('<tr>');
					html.push('<td style="text-align: center;">' + product.productName + '</td>');
					html.push('<td style="text-align: center;"><img width="40" src="' + ctx + product.image1 + '"></td>');
					html.push('<td style="text-align: center;">' + n.buyCount + '</td>');
					html.push('<td style="text-align: center;">￥' + n.buyPrice + '</td>');
					html.push('</tr>');
				});
				$("#detail-product-list tbody tr:eq(0)").after(html.join(''));
				
				switch (order.status) {
				case 0: 
					status = "待支付预付款"; 
					$("#order-del").show();
					break;
				case 1: status = "已支付预付款";
					$("#alipaySerialDiv").show();
					$("#alipaySerial").focus();
					$("#alipaySerial").blur();
					$("#alipaySerialSpanDiv").show();
					
					break;
				case 3: status = "已完成"; 
					$("#documentSerialDiv").show();
					$("#documentSerial").focus();
					$("#documentSerial").blur();
					$("#alipaySerialSpanDiv").show();
					$("#documentSerialSpanDiv").show();
					break;
				case 99: status = "已退订";break;
			}
			} else {
				$.alert({message:data.message});
			}
		},
		error : function() {
			$('.dialog-win').unmask();
		}
	});
}

function clearDetail () {
	$("#orderNum").html("");
	//$("#orderStatus").html("");
	$("#orderTime").html("");
	$("#payTime").html("");
	$("#fullAmount").html("");
	$("#preAmount").html("");
	$("#alipayAccount").html("");
	$("#contactName").html("");
	$("#contactMobile").html("");
	$('.dialog-win').unmask();
	$("#order-body").hide();
	$("#order-del").hide();
	$("#alipaySerial").val("");
	$("#documentSerial").val("");
	$("#alipaySerialDiv").hide();
	$("#documentSerialDiv").hide();
	
	$.each($("#detail-product-list tbody tr"), function (i, n) {
		if (i > 0) {
			$(n).remove();
		}
	});
}